package hmis.pos.sys.controller;

import hmis.pos.Common.pojo.ApiResponse;
import hmis.pos.sys.entity.SysUserEntity;
import hmis.pos.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("user")
public class SysUserController {

    //  @RequiresAuthentication
    //  @RequiresRoles("admin")
    //  @RequiresPermissions(logical = Logical.AND, value = {"view", "edit"})


    @Autowired
    private SysUserService sysUserService;

    @PostMapping("findAll")
    @ResponseBody
    public List<SysUserEntity> findAll(){
        return sysUserService.findAll();
    }

    @PostMapping("save")
    @ResponseBody
    public ApiResponse<SysUserEntity> save(@RequestBody SysUserEntity sysUserEntity){
        SysUserEntity entity = sysUserService.save(sysUserEntity);
        if(entity==null){
            return new ApiResponse<>(401,"用户名已存在");
        }
        else {
            return new ApiResponse<>(200,"添加成功");
        }
    }

    @PostMapping("findById")
    @ResponseBody
    public SysUserEntity findById(@RequestBody SysUserEntity sysUserEntity){
        SysUserEntity entity = sysUserService.findById(sysUserEntity.getId());
        if(entity!=null){
            return entity;
        }
        else {
            return null;
        }
    }

    @PostMapping("update")
    @ResponseBody
    public ApiResponse<SysUserEntity> update(@RequestBody SysUserEntity sysUserEntity){
        SysUserEntity entity = sysUserService.update(sysUserEntity);
        if(entity==null){
            return new ApiResponse<>(401,"修改失败");
        }
        else {
            return new ApiResponse<>(200,"修改成功");
        }
    }

    @PostMapping("deleteById")
    @ResponseBody
    public ApiResponse<SysUserEntity> deleteById(@RequestBody SysUserEntity sysUserEntity){
        boolean temp = sysUserService.delete(sysUserEntity);
        if(temp){
            return new ApiResponse<>(200,"修改成功");
        }
        else {
            return new ApiResponse<>(401,"修改失败");
        }
    }

}